package cn.wildfire.chat.kit.contact.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.user.UserViewModel;
import cn.wildfire.chat.kit.utils.PinyinUtils;
import cn.wildfirechat.model.UserInfo;

public class UIUserInfo implements Parcelable {

    private String category = "";
    // 用来排序的字段
    private String sortName;
    private boolean showCategory;
    private UserInfo userInfo;
    private boolean isChecked;
    private boolean isCheckable = true;

    public UIUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    protected UIUserInfo(Parcel in) {
        category = in.readString();
        sortName = in.readString();
        showCategory = in.readByte() != 0;
        userInfo = in.readParcelable(UserInfo.class.getClassLoader());
        isChecked = in.readByte() != 0;
        isCheckable = in.readByte() != 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(category);
        dest.writeString(sortName);
        dest.writeByte((byte) (showCategory ? 1 : 0));
        dest.writeParcelable(userInfo, flags);
        dest.writeByte((byte) (isChecked ? 1 : 0));
        dest.writeByte((byte) (isCheckable ? 1 : 0));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<UIUserInfo> CREATOR = new Creator<UIUserInfo>() {
        @Override
        public UIUserInfo createFromParcel(Parcel in) {
            return new UIUserInfo(in);
        }

        @Override
        public UIUserInfo[] newArray(int size) {
            return new UIUserInfo[size];
        }
    };

    public void setCategory(String category) {
        this.category = category;
    }

    public void setShowCategory(boolean showCategory) {
        this.showCategory = showCategory;
    }

    public String getCategory() {
        return category;
    }

    public boolean isShowCategory() {
        return showCategory;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String displayNamePinyin) {
        this.sortName = displayNamePinyin;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public boolean isCheckable() {
        return isCheckable;
    }

    public void setCheckable(boolean checkable) {
        isCheckable = checkable;
    }

    /**
     * 转换为界面的显示数据对象
     *
     * @param userInfo
     * @return
     */
    public static UIUserInfo userInfoToUIUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            return null;
        }
        UIUserInfo info = new UIUserInfo(userInfo);
        try {
            String indexLetter;
            UserViewModel userViewModel = WfcUIKit.getAppScopeViewModel(UserViewModel.class);
            String displayName = userViewModel.getUserDisplayName(userInfo);
            if (!TextUtils.isEmpty(displayName)) {
                String pinyin = PinyinUtils.getPinyin(displayName);
                char c = pinyin.toUpperCase().charAt(0);
                if (c >= 'A' && c <= 'Z') {
                    indexLetter = c + "";
                    info.setSortName(pinyin);
                } else {
                    indexLetter = "#";
                    // 为了让排序排到最后
                    info.setSortName("{" + pinyin);
                }
                info.setCategory(indexLetter);
            } else {
                info.setSortName("");
            }
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 根据字母重新排序显示
     *
     * @param userInfos
     * @return
     */
    public static List<UIUserInfo> userInfoToUIUserInfo(List<UserInfo> userInfos) {
        if (userInfos != null) {
            List<UIUserInfo> uiUserInfos = new ArrayList<>(userInfos.size());
            String indexLetter;
            for (UserInfo userInfo : userInfos) {
                uiUserInfos.add(userInfoToUIUserInfo(userInfo));
            }
            Collections.sort(uiUserInfos, (o1, o2) -> o1.getSortName().compareToIgnoreCase(o2.getSortName()));

            String preIndexLetter = null;
            for (UIUserInfo info : uiUserInfos) {
                indexLetter = info.getCategory();
                if (preIndexLetter == null || !preIndexLetter.equals(indexLetter)) {
                    info.setShowCategory(true);
                }
                preIndexLetter = indexLetter;
            }
            return uiUserInfos;
        } else {
            return null;
        }
    }

}
